#!/bin/sh /etc/rc.common

START=19

mwan3_add_one() {
	local intf=$1
	uci set mwan3.${intf}=interface
	uci set mwan3.${intf}.enabled='1'
	uci set mwan3.${intf}.initial_state='online'
	uci set mwan3.${intf}.family='ipv4'
	uci delete mwan3.${intf}.track_ip
	uci add_list mwan3.${intf}.track_ip='1.1.1.1'
	uci add_list mwan3.${intf}.track_ip='114.114.114.114'
	uci add_list mwan3.${intf}.track_ip='119.29.29.29'
	uci add_list mwan3.${intf}.track_ip='8.8.8.8'
	uci set mwan3.${intf}.track_method='ping'
	uci set mwan3.${intf}.reliability='1'
	uci set mwan3.${intf}.count='1'
	uci set mwan3.${intf}.size='56'
	uci set mwan3.${intf}.max_ttl='60'
	uci set mwan3.${intf}.check_quality='0'
	uci set mwan3.${intf}.timeout='2'
	uci set mwan3.${intf}.interval='5'
	uci set mwan3.${intf}.failure_interval='5'
	uci set mwan3.${intf}.recovery_interval='5'
	uci set mwan3.${intf}.down='3'
	uci set mwan3.${intf}.up='3'
	uci set mwan3.${intf}.flush_conntrack='never'

	uci set mwan3.${intf}_m100_w100=member
	uci set mwan3.${intf}_m100_w100.interface="${intf}"
	uci set mwan3.${intf}_m100_w100.metric='100'
	uci set mwan3.${intf}_m100_w100.weight='100'

	uci set mwan3.balanced=policy
	uci set mwan3.balanced.last_resort='unreachable'
	uci add_list mwan3.balanced.use_member="${intf}_m100_w100"
}

add_to_zone_wan() {
	local idx=0

	while uci get firewall.@zone[$idx] >/dev/null 2>&1; do
		if [ "$(uci get firewall.@zone[$idx].name)" = "wan" ]; then
			networks=`uci get firewall.@zone[$idx].network`;
			for nw in $networks; do
				[ "$nw" = "$1" ] && return
			done
			uci set firewall.@zone[$idx].network="`echo $networks $1`"
			uci commit firewall
			return
		fi
		idx=$((idx + 1))
	done
}

add_xwan() {
	local cfg="$1"
	local enabled number balanced
	local ifname idx
	local change=0

	config_get enabled "$cfg" enabled
	config_get number "$cfg" number
	config_get balanced "$cfg" balanced

	xwan_new=$(uci show xwan | md5sum | head -c32)
	xwan_old=$(uci get mwan3.globals.xwan_md5)

	[ "x$enabled" = "x1" ] && {
		[ "x${xwan_new}" = "x${xwan_old}" ] || {
			while uci delete mwan3.@interface[0] >/dev/null 2>&1; do :; done
			while uci delete mwan3.@member[0] >/dev/null 2>&1; do :; done
			while uci delete mwan3.@policy[0] >/dev/null 2>&1; do :; done
			change=1
		}
	}

	idx=0
	ifname=`uci get network.wan.ifname`

	if [ "x$enabled" = "x1" ] && test $number -ge 2 && test -n "$ifname"; then
		uci get network.wan.metric >/dev/null 2>&1 || {
			uci set network.wan.metric=40
			change=1
		}
		[ "x$balanced" = "x1" ] && {
			uci get mwan3.wan >/dev/null 2>&1 || {
				mwan3_add_one wan
				change=1
			}
		}
		while :; do
			idx=$((idx + 1))
			uci get network.device$idx >/dev/null 2>&1 || {
				uci set network.device$idx=device
				uci set network.device$idx.ifname="$ifname"
				uci set network.device$idx.type="macvlan"
				uci set network.device$idx.name="mvlan$idx"
				change=1
			}

			uci get network.xwan$idx >/dev/null 2>&1 || {
				uci set network.xwan$idx=interface
				uci show network.wan | grep "network\.wan\." | tr ".\'" "==" | cut -d= -f3,5 | sed 's/=/ /' | while read _k _v; do
					uci set network.xwan$idx.$_k="$_v"
				done
				uci set network.xwan$idx.ifname="mvlan$idx"
				uci set network.xwan$idx.metric=$((40 + idx))
				change=1
			}

			add_to_zone_wan xwan$idx

			[ "x$balanced" = "x1" ] && {
				uci get mwan3.xwan$idx >/dev/null 2>&1 || {
					mwan3_add_one xwan$idx
					change=1
				}
			}
			test $idx -ge $((number - 1)) && break
		done
	fi
	
	#clean up
	idx=$((idx + 1))
	while :; do
		uci get network.device$idx >/dev/null 2>&1 && {
			uci delete network.device$idx
			change=1
		}
		uci get network.xwan$idx >/dev/null 2>&1 && {
			uci delete network.xwan$idx
			change=1
		}
		idx=$((idx + 1))
		uci get network.device$idx >/dev/null 2>&1 || uci get network.xwan$idx >/dev/null 2>&1 || break
	done

	[ "x$change" = "x1" ] && {
		uci commit network
		uci set mwan3.globals.xwan_md5="${xwan_new}"
		uci commit mwan3
		[ "x$XRELOAD" = "x1" ] && {
			/etc/init.d/network reload
			/etc/init.d/firewall reload
			/etc/init.d/mwan3 reload
		}
	}
}

start() {
	config_load xwan
	config_foreach add_xwan xwan
}

restart() {
	XRELOAD=1 start
}
